package com.garlic.face.jlf.presenter;

import android.text.TextUtils;

import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.ServiceInterface;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author wenhui02.liu
 * @date 2018/8/13.
 */

public class PayFailPresenter implements PayFailContract.Presenter {
    String TAG = "PayFailPresenter";
    PayFailContract.View mView;

    public PayFailPresenter(PayFailContract.View view) {
        mView = view;
    }


    @Override
    public void cancelOrder(String reason) {
        ServiceInterface.getInstance().cancelTrade("", "");
    }

    @Override
    public void parseJson(JSONObject jsonObject) {
        String interfacetype = "";
        String retflag = "";
        String retmsg = "";
        try {
            interfacetype = jsonObject.getString("INTERFACETYPE");
            retflag = jsonObject.getString("retflag");
            retmsg = jsonObject.getString("retmsg");
            if (interfacetype.equals(JLFConstant.TAG_CANCELTRADE_RESPONSE)) {
                mView.hideProgress();
                if (retflag.equals("0")) {
                    Tools.clearTrade();
                    mView.gotoMain();
                } else {
                    ToastUtils.toastShow(retmsg, ToastUtils.ERROR);
                    LogUtils.e("取消交易" + retmsg);
                }
            }else if (interfacetype.equals(JLFConstant.TAG_SUPERVISOR_RESPONSE)) {
                retflag = jsonObject.getString("retflag");
                retmsg = jsonObject.getString("retmsg");
                //TODO 刷卡输完管理员账号密码返回可能retflag为1，return retmsg为100000
                if (retflag.equals("0")) {
                    if (jsonObject.has("receiptLines")){
                        String receiptLines = jsonObject.getString("receiptLines");
                        if(!TextUtils.isEmpty(receiptLines)) {
                            String[] receiptArray = receiptLines.replace("[", "").replace("]", "").split(",");
                            ArrayList<String> receiptList = new ArrayList<String>();
                            for (int i = 0; i < receiptArray.length; i++) {
                                receiptList.add(receiptArray[i]);
                            }
                            Means.getInstance().setPrintBillHead(receiptList);
                        }
                    }
                    mView.printText();
                    cancelOrder("支付错误页面取消交易[自动][部分支付]");
                } else {
                    if (jsonObject.has("receiptLines")){
                        String receiptLines = jsonObject.getString("receiptLines");
                        if(!TextUtils.isEmpty(receiptLines)) {
                            String[] receiptArray = receiptLines.replace("[", "").replace("]", "").split(",");
                            ArrayList<String> receiptList = new ArrayList<String>();
                            for (int i = 0; i < receiptArray.length; i++) {
                                receiptList.add(receiptArray[i]);
                            }
                            Means.getInstance().setPrintBillHead(receiptList);
                            mView.printText();
                        }
                    }
                    cancelOrder("支付错误页面取消交易[自动][部分支付]");
                    ToastUtils.toastShow(retmsg, ToastUtils.ERROR);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
